﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.Areas.Customer.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/Customer/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult IndexAdd()
        {
            return View();
        }

        public ActionResult IndexEdit()
        {
            return View();
        }

        public void add()
        {
            string data = HttpUtility.UrlDecode(Request.Form.Get("add"));
            IEnumerable<T_C_Customers> add = JsonHelp.ConvertJsonToEntity<IEnumerable<T_C_Customers>>(data);

            Response.Write(new BLLT_C_Customers().Add(add));
        }

        public void Save()
        {
            string data = HttpUtility.UrlDecode(Request.Form.Get("edit"));
            Guid CustomerID = Guid.Parse(Request.Form.Get("CustomerID"));
            IEnumerable<T_C_Customers> edit = JsonHelp.ConvertJsonToEntity<IEnumerable<T_C_Customers>>(data);
            edit.ToList().ForEach(a => a.CustomerID = CustomerID);
            Response.Write(new BLLT_C_Customers().Save(edit));
        }

        public void Get()
        {
            string xml = JsonHelp.GetParaXML(Request.Form.Get("pageIndex"), Request.Form.Get("pageSize"),
               Request.Form.Get("sortField"), Request.Form.Get("sortOrder"), Request.Form.Get("key"));

            Response.Write(JsonHelp.EntitiesToString(new BLLT_C_Customers().Get(xml)));
        }

        public void Del()
        {
            string json = @"[]";

            IEnumerable<T_C_Customers> del = Request.Form.Get("del") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_C_Customers>>(Request.Form.Get("del"));

            Response.Write(new BLLT_C_Customers().Del(del));
        }

        public void GetOne()
        {
            string CustomerID = Request.Form.Get("CustomerID");

            Response.Write(JsonHelp.EntitiesToString(new BLLT_C_Customers().GetOne(CustomerID)));
        }



    }
}
